package com.example.sharesinfo.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

public class WordExplainActivity extends BaseUIActivity {
    private static final int GetExplainSuccess = 1001;
    private static final int GetExplainFailed = 1002;
    private ImageButton iv_back;
    private EditText et_search;
    private TextView tv_search;
    private TextView tv_word;
    private TextView tv_explain;
    //返回的json字符串
    private String result;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GetExplainSuccess:
                    JSONObject result = (JSONObject) msg.obj;
                    try {
                        tv_word.setText(result.getString("Word"));
                        et_search.setText(result.getString("Word"));
                        tv_explain.setText(result.getString("Explain"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case GetExplainFailed:
                    Toast.makeText(WordExplainActivity.this, "暂无该词解释", Toast.LENGTH_SHORT).show();
                    break;
                case 404:
                    break;
            }
            return false;
        }
    });

    public static void startActivity(Context mContext, String result) {
        Intent intent = new Intent(mContext, WordExplainActivity.class);
        intent.putExtra("result", result);
        mContext.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_explain);
        initData();
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_search = findViewById(R.id.et_search);
        tv_search = findViewById(R.id.tv_search);
        tv_word = findViewById(R.id.tv_word);
        tv_explain = findViewById(R.id.tv_explain);
        try {
            JSONObject object = new JSONObject(result);
            tv_word.setText(object.getString("Word"));
            et_search.setText(object.getString("Word"));
            tv_explain.setText(object.getString("Explain"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputText = et_search.getText().toString().trim();
                if (TextUtils.isEmpty(inputText)) {
                    return;
                }
                getExplain(inputText);
            }
        });
    }

    /**
     * @type Get
     * @explain 获取词典词条解释（参数为Word和UserId）
     */
    private void getExplain(String inputText) {
        String url = "http://119.23.221.142/api/customer/GetExplain?Word=" + inputText + "&UserId="
                + SpUtils.getInstance().getString(Constants.SP_USER_ID, "");
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    message.what = GetExplainSuccess;
                    message.obj = object;
                    mHandler.sendMessage(message);
                } else {
                    message.what = GetExplainFailed;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        result = getIntent().getStringExtra("result");
    }
}
